package reflect;

import reflect.annotations.AutoRunClass;

import java.io.File;
import java.net.URISyntaxException;

/**
 * 实例化与当前类Test8在同一包中被@AutoRunClass标注的类
 */
public class Test8 {
    public static void main(String[] args) throws URISyntaxException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class c = Test8.class;
        String pack = c.getPackage().getName();

        File[] subs = new File(Test8.class
                              .getResource(".").toURI())
                              .listFiles(f->f.getName().endsWith(".class"));
        for(File sub : subs) {
            String fileName = sub.getName();
            String className = fileName.replace(".class",".");
            Class cls = Class.forName(pack+"."+className);
            if(cls.isAnnotationPresent(AutoRunClass.class)){
                System.out.println("被@AutoRunClass标注的类的实例化:"+cls.newInstance());
            }
        }
    }
}
